package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.config.FileTemplate;
import net.qhzw.vip.qiyu.entity.DtsBarManageSecondary;
import net.qhzw.vip.qiyu.handler.MyException;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.DtsBarManageSecondaryService;
import net.qhzw.vip.qiyu.service.DtsBarManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("dts/barManageSecondary")
@Api(tags = "电柜维护管理")
public class DtsBarManageSecondaryController {


    @Autowired
    private DtsBarManageSecondaryService dtsBarManageSecondaryService;
    @Autowired
    private FileTemplate fileTemplate;




    @ApiOperation(value = "分页查询维修维护")
    @GetMapping
    public JsonResult<Page<DtsBarManageSecondary>> findByPage(Page<DtsBarManageSecondary> page,DtsBarManageSecondary dtsBarManageSecondary) {
        //Long shopId = SecurityUtils.getLoginUser().getShopId();
        Page<DtsBarManageSecondary> dtsBarManageSecondary1 = dtsBarManageSecondaryService.findByPage(page,dtsBarManageSecondary);
        return JsonResult.ok(dtsBarManageSecondary1);
    }

    @ApiOperation(value = "根据ID查询电柜维护")
    @GetMapping("{id}")
    public JsonResult<DtsBarManageSecondary> findDtsBarManage(@PathVariable Long id) {
        DtsBarManageSecondary dtsBarManageSecondary = dtsBarManageSecondaryService.getById(id);
        return JsonResult.ok(dtsBarManageSecondary);
    }


    @ApiOperation(value = "添加故障禁仓信息")
    @PostMapping
    public JsonResult addBar(@RequestBody DtsBarManageSecondary dtsBarManageSecondary) {

        boolean save = dtsBarManageSecondaryService.addBar(dtsBarManageSecondary);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "小程序添加修复故障禁仓信息")
    @PostMapping("/secondary")
    public JsonResult addBarManageSecondary(@RequestBody DtsBarManageSecondary dtsBarManageSecondary) {

        boolean save = dtsBarManageSecondaryService.addBarManageSecondary(dtsBarManageSecondary);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改电柜维护")
    @PutMapping
    public JsonResult updateDtsBarManage(@RequestBody DtsBarManageSecondary dtsBarManageSecondary) {
        boolean save = dtsBarManageSecondaryService.updateDtsBarManage(dtsBarManageSecondary);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "设置电柜维修状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody DtsBarManageSecondary dtsBarManageSecondary) {
        boolean save = dtsBarManageSecondaryService.updateDtsBarManage(dtsBarManageSecondary);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除维修维护")
    @DeleteMapping("{id}")
    public JsonResult<DtsBarManageSecondary> findDtsBarManage(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        boolean remove = dtsBarManageSecondaryService.removeByIds(list);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }
    @ApiOperation(value = "表格导入电柜信息值")
    @PostMapping("importBar")
    public JsonResult importRecharge(@RequestParam("file") MultipartFile file) {
        //Long shopId = SecurityUtils.getLoginUser().getShopId();
        //if (shopId == 1) {
        //    throw new ApiException("您不能充值，请登录商户账号进行操作！");
        //}
         dtsBarManageSecondaryService.importRecharge(file);
        return JsonResult.ok();
    }

    //@ApiOperation(value = "上传文件")
    //@PostMapping("upload")
    //public JsonResult upload(@RequestBody DtsBarManageSecondary dtsBarManageSecondary) {
    //    boolean update = false;
    //
    //    if (dtsBarManageSecondary.getId() != null && dtsBarManageSecondary.getRemark() != null){
    //        update = dtsBarManageSecondaryService.updateById(dtsBarManageSecondary);
    //    }
    //    if (update) {
    //        return JsonResult.ok();
    //    }
    //    return JsonResult.fail();
    //}



    /**
     * @description: 批量下载
     * @date: 2022/8/17 16:50
     * @param: filenames: 多个文件名称
     * @Param: zip: 压缩包名称
     * @Param: res: 响应对象
     * @return: void
     **/
    @ApiOperation(value = "下载照片压缩文件")
    @GetMapping("batchDownload")
    public void batchDownload( HttpServletResponse res, HttpServletRequest req,@RequestParam(value = "created",required =false) String created) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        if(created == null && StringUtils.isEmpty(created)){
            created =  formatter.format(new Date());
        }

        List<DtsBarManageSecondary> list = dtsBarManageSecondaryService.list(new LambdaQueryWrapper<DtsBarManageSecondary>()
                .apply(
                        "date_format(created,'%Y-%m') = date_format({0},'%Y-%m')", created)
                );
        //Map<String, String> collect = list.stream().collect(Collectors.toMap(DtsBarManageSecondary::getPanorama, DtsBarManageSecondary::getPicture));

        //List<String> listKeyStream = collect.keySet().stream().collect(Collectors.toList());
        //List orderNoList=list.stream().map(DtsBarManageSecondary::getPicture).collect(Collectors.toList());
        List<String> filenames = list.stream()
                .filter(i -> i.getPicture() != null && !i.getPicture().isEmpty())
                .map(i -> i.getPicture())
                .collect(Collectors.toList());
        List<String> filenames1 = list.stream()
                .filter(i -> i.getPanorama() != null && !i.getPanorama().isEmpty())
                .map(i -> i.getPanorama())
                .collect(Collectors.toList());
        filenames.addAll(filenames1);
        if (filenames.size() < 1) {
         throw new MyException("暂无照片");
        }

        String zipName = formatter.format(new Date()); //+ "故障禁仓处理照片";

        fileTemplate.batchDownload(filenames, zipName, res, req);

    }

    //查看修理电柜详细
    @ApiOperation(value = "根据ID查询电柜维护详细信息")
    @GetMapping("findSecondary/{id}")
    public JsonResult findDtsBarManageSecondary(@PathVariable Long id) {
        List<DtsBarManageSecondary> list = dtsBarManageSecondaryService.list(new LambdaQueryWrapper<DtsBarManageSecondary>()
                .eq(id != null && !StringUtils.isEmpty(id), DtsBarManageSecondary::getDtsBarId, id));
        for (DtsBarManageSecondary dtsBarManageSecondary : list) {
            String fileName =  "qiyu"+"/" + dtsBarManageSecondary.getPicture();
            String fileName1 =  "qiyu"+"/" + dtsBarManageSecondary.getPanorama();
            dtsBarManageSecondary.setPicture(fileName);
            dtsBarManageSecondary.setPanorama(fileName1);
        }
        return JsonResult.ok(list);
    }

    /**
     * 导出修复故障禁仓
     */
    @ApiOperation(value = "导出修复故障禁仓")
    @GetMapping("exportSecondary")
    public void exportSecondary( DtsBarManageSecondary dtsBarManageSecondary, HttpServletResponse response) throws IOException {
        dtsBarManageSecondaryService.exportSecondary(response,dtsBarManageSecondary);
    }






}


